﻿using System;       
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Modeling_TeacherPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        if (Session["WelcomeUser"] != null)
        {
            if (Session["StudentID"] != null)
            {
                if (udm.createConnection() != null)
                {
                    string id = Session["StudentID"].ToString();

                    grvContent.DataSource = udm.showCoursesRegisByStudentID(udm.createConnection(),1, id);
                    grvContent.DataBind();

                    if (grvContent.Rows.Count == 0)
                    {
                        lblNone.Text = "(None)";
                    }

                    GridView1.DataSource = udm.showCoursesRegisByStudentID(udm.createConnection(),2, id);
                    GridView1.DataBind();

                    if (GridView1.Rows.Count == 0)
                    {
                        lblNone2.Text = "(None)";
                    }
                }
                else
                {
                    Response.Redirect("~/ErrorPage.aspx");
                }
            }
            else
            {
                Response.Redirect("~/LoginMessage.aspx?id=1");
            }
        }
        else
        {
            Response.Redirect("~/Login.aspx?id=1");
        }
    }
    protected void grvContent_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // reference the Delete LinkButton
            LinkButton db = (LinkButton)e.Row.Cells[0].Controls[0];

            string id = e.Row.Cells[1].Text.ToString();

            db.OnClientClick = string.Format("return confirm('Are you certain you want to remove the sign up ID = {0}?')", id);
        }
    }
    protected void grvContent_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        int id = Convert.ToInt32(grvContent.Rows[0].Cells[1].Text.ToString());

        if (udm.removeStudentRegistrationOfCourse(udm.createConnection(),id))
        {
            string idStu = Session["StudentID"].ToString();

            grvContent.DataSource = udm.showCoursesRegisByStudentID(udm.createConnection(), 1, idStu);
            grvContent.DataBind();
        }
        else
        {
            Response.Redirect("~/ErrorPage.aspx");
        }
    }
}